package cn.glutnn.subject.domain.handler.subject;

import cn.glutnn.subject.common.enums.SubjectInfoTypeEnum;
import cn.glutnn.subject.domain.convert.SubjectRadioConverter;
import cn.glutnn.subject.domain.enyity.SubjectAnswerBO;
import cn.glutnn.subject.domain.enyity.SubjectInfoBO;
import cn.glutnn.subject.domain.enyity.SubjectOptionBO;
import cn.glutnn.subject.infra.basic.entity.SubjectRadio;
import cn.glutnn.subject.infra.basic.service.SubjectRadioService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/11/28 下午2:49
 * Description:
 */

@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 单选处理逻辑
        List<SubjectRadio> subjectRadioList = new LinkedList<>();

        subjectInfoBO.getOptionList().forEach(subjectAnswerBO -> {
            SubjectRadio subjectRadio = SubjectRadioConverter.INSTANCE.convertBoToEntity(subjectAnswerBO);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadioList.add(subjectRadio);
        });

        subjectRadioService.saveBatch(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        LambdaQueryWrapper<SubjectRadio> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubjectRadio::getSubjectId, subjectId);
        List<SubjectRadio> subjectRadios = subjectRadioService.list(wrapper);
        SubjectOptionBO optionBO = new SubjectOptionBO();
        LinkedList<SubjectAnswerBO> list = new LinkedList<>();
        subjectRadios.forEach(subjectRadio -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setOptionType(subjectRadio.getOptionType());
            subjectAnswerBO.setOptionContent(subjectRadio.getOptionContent());
            subjectAnswerBO.setIsCorrect(subjectRadio.getIsCorrect());
            list.add(subjectAnswerBO);
        });
        optionBO.setOptionList(list);
        return optionBO;
    }
}
